package cn.simon.controller;

import cn.simon.model.GoodsClassify;
import cn.simon.model.GoodsClassifySec;
import cn.simon.service.GoodsClassifySecService;
import cn.simon.service.GoodsClassifyService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
@Log
@Controller
@RequestMapping("/goodsClassify")
public class GoodsClassifyController {
    /**
     * 分类列表
     * @return
     */
    @Autowired
    private GoodsClassifyService goodsClassifyService;
    @Autowired
    private GoodsClassifySecService goodsClassifySecService;
    @GetMapping("list")
    public String classifyList(Model model){
        List<GoodsClassify> goodsClassify = goodsClassifyService.getAllGoodsClassify();
        model.addAttribute("list",goodsClassify);
        return "/classifyList";
    }
    /**
     * 查看二级分类
     */
    @GetMapping("findSec")
    public String ClassifySecList(Model model,Integer id){
        List<GoodsClassifySec> classifySec = goodsClassifySecService.findSecById(id);
        model.addAttribute("secList",classifySec);
        return "classifySecList";
    }
    /**
     * 添加一级分类
     */
    @GetMapping("/add")
    public String addClassify(){
        return "addClassify";
    }
    @PostMapping("/add")
    public String addClassify(GoodsClassify goodsClassify){
        goodsClassifyService.addClassify(goodsClassify);
        return "addClassify";
    }
    /**
     * 修改一级分类
     */
    @GetMapping("/update")
    public String updateClassify(Model model,Integer id){
        GoodsClassify classify = goodsClassifyService.findById(id);
        model.addAttribute("classify",classify);
        log.info("------------------------------------"+classify.toString());
        return "updateClassify";
    }
    @PostMapping("/update")
    public String updateClassify(GoodsClassify goodsClassify,Model model){
        model.addAttribute("classify",goodsClassify);
        log.info("------------------------------------"+goodsClassify.toString());
        goodsClassifyService.updateClassify(goodsClassify);
        return "redirect:/goodsClassify/list";
    }
    /**
     * 删除一级分类(与其中的二级分类)
     */

    @GetMapping("/delete")
    public String deleteClassify(Integer id){
        goodsClassifyService.deleteClassifyById(id);
        return "redirect:/goodsClassify/list";
    }

}
